package youbff.cloud.router.filter;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;

import youbff.cloud.core.enums.CloudCommonBoolean;
import youbff.cloud.core.utils.LogUtils;
import youbff.cloud.core.utils.ServerUtils;
import youbff.cloud.core.utils.StringUtils;
import youbff.cloud.router.enums.RouterFilterTypeEnum;
import youbff.cloud.router.utils.RequestUtils;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

/**
 * 签名过滤
 * 
 * @author lixuefeng
 * @version 1.0
 * @email youbff@163.com
 * @createTime 2017年10月24日上午9:58:27
 */
@Component
@RefreshScope
public class CloudRouterLoggerPostFilter extends ZuulFilter {

    private Logger logger = Logger.getLogger(CloudRouterLoggerPostFilter.class);
    private static String methodName = "日志过滤";
    @Value("${myconfig.router.log.switch}")
    private String logSwitch;
    @Value("${myconfig.router.log.exclude}")
    private String logExclude;

    @Override
    public boolean shouldFilter() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        boolean isSuccess = (boolean) ctx.get("isSuccess");
        if (!isSuccess) {
            return isSuccess;
        }
        if (CloudCommonBoolean.FALSE.getCode().equals(logSwitch)) {
            ctx.set("isSuccess", true);
            return false;
        }
        String servletPath = request.getServletPath();
        String[] signExcludeArray = logExclude.split(",");
        for (String s : signExcludeArray) {
            if (s.equalsIgnoreCase(servletPath)) {
                ctx.set("isSuccess", true);
                return false;
            }
        }
        ctx.set("isSuccess", true);
        return true;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public String filterType() {
        return RouterFilterTypeEnum.POST.getCode();
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        Map<String, Object> params = RequestUtils.getRequestParams(ctx);
        InputStream stream = ctx.getResponseDataStream();
        try {
            String body = StreamUtils.copyToString(stream,
                    StandardCharsets.UTF_8);
            if (StringUtils.isBlank(body)) {
                body = ctx.getResponseBody();
            }
            ctx.setResponseBody(body);
            logger.info(LogUtils.getLogInfo(methodName,"日志记录","记录日志;客户端ip:" + ServerUtils.getClientIpAddr(request)+";请求接口:"+request.getServletPath()+";返回结果:"+body,
                    params.toString(), null));
        } catch (IOException e) {
        }
        return null;
    }

}
